import router from "@/router";

import {
  getToken,
  setToken,
  removeToken,
  setUserMess,
  getUserMess,
  removeUserMess
} from "@/utils/auth";

const user = {
  state: {
    user: getUserMess(),
    token: getToken(),
    socketOpen: false
  },

  mutations: {
    SET_USER: (state, user) => {
      state.user = user;
    }
  },

  actions: {
    // 用户名登录
    LoginByUsername({ commit }, user) {
      console.log("user", user);
      commit("SET_USER", user);
      setToken(user.token);
      setUserMess(user);
    },

    // 登出
    LogOut({ commit }) {
      return new Promise(resolve => {
        commit("SET_USER", "");
        sessionStorage.clear();
        router.push({ path: "/login" });
        removeToken();
        removeUserMess();
        resolve();
      });
    }
  }
};
export default user;
